//
//  UserCenterHomeTableViewCell.m
//  much
//
//  Created by Believer Just on 2019/1/8.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "UserCenterHomeTableViewCell.h"
#import "UserCenterHomeTVCCollectionViewCell.h"

@interface UserCenterHomeTableViewCell () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

/** 标题背景视图 */
@property (nonatomic, strong) UIView *titleBgView;
/** 标题 */
@property (nonatomic, strong) UILabel *titleLabel;
/** 右侧标题 */
@property (nonatomic, strong) UILabel *rightTitleLabel;
/** 右侧图片 */
@property (nonatomic, strong) UIImageView *rightArrowImageView;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation UserCenterHomeTableViewCell

+ (instancetype)initTableView:(UITableView *)tableView {
    UserCenterHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UserCenterHomeTableViewCell class])];
    if (!cell) {
        cell = [[UserCenterHomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([UserCenterHomeTableViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}


#pragma mark - method
/** 隐藏标题视图/右侧视图 */
- (void)hiddenTitleBgView:(BOOL)hiddenTitleBgView hiddenRightView:(BOOL)hiddenRightView {
    self.titleBgView.hidden = hiddenTitleBgView;
    if (!hiddenTitleBgView) {
        self.titleBgView.hidden = NO;
        [self.titleBgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(44.f);
        }];
        self.rightTitleLabel.hidden = self.rightArrowImageView.hidden = hiddenRightView;
    } else {
        [self.titleBgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.01f);
        }];
    }
}


#pragma mark - setting
- (void)setDic:(NSMutableDictionary *)dic {
    _dic = dic;
    
    self.titleLabel.text = _dic[SectionTitle];
    switch ([_dic[SectionTag] integerValue]) {
        case UCHVSectionTypeNormal:
            [self hiddenTitleBgView:YES hiddenRightView:YES];
            break;
        case UCHVSectionTypeOrder:
            [self hiddenTitleBgView:NO hiddenRightView:NO];
            break;
        case UCHVSectionTypeAssistant:
            [self hiddenTitleBgView:NO hiddenRightView:YES];
            break;
        default:
            break;
    }
    
    [self.collectionView reloadData];
}


#pragma mark - <UICollectionViewDataSource, UICollectionViewDelegate>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.dic[SectionCellArray] count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UserCenterHomeTVCCollectionViewCell *collectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:[UserCenterHomeTVCCollectionViewCell cellClass] forIndexPath:indexPath];
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:self.dic[SectionCellArray][indexPath.row]];
    [collectionCell setCollectionCellData:dic];
    
    return collectionCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    if ([self.delegateCollectionViewDelegate respondsToSelector:@selector(clickCollectionRow:)]) {
        [self.delegateCollectionViewDelegate clickCollectionRow:[[self.dic[SectionCellArray][indexPath.row] objectForKey:CellTag] integerValue]];
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.dic[SectionTag] integerValue] == UCHVSectionTypeNormal) {
        return CGSizeMake(ScreenWidth / 4 -0.5 *4, 75);
    }
    return CGSizeMake(ScreenWidth / 5 -0.5 *5, 75);
}


#pragma mark - 懒加载
- (UIView *)titleBgView {
    if (!_titleBgView) {
        _titleBgView = [UIView new];
        _titleBgView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_titleBgView];
        [_titleBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.and.top.equalTo(self.contentView);
            make.height.mas_equalTo(44);
        }];
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor = Color217;
        [self.titleBgView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.titleBgView);
            make.left.equalTo(self.titleBgView).with.offset(15);
            make.right.equalTo(self.titleBgView).with.offset(- 15);
            make.height.mas_equalTo(1);
        }];
    }
    return _titleBgView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = Color34;
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = APPFONT(14);
        [self.titleBgView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleBgView).with.offset(15);
            make.centerY.equalTo(self.titleBgView);
        }];
    }
    return _titleLabel;
}
- (UILabel *)rightTitleLabel {
    if (!_rightTitleLabel) {
        _rightTitleLabel = [UILabel new];
        _rightTitleLabel.textColor = Color140;
        _rightTitleLabel.textAlignment = NSTextAlignmentRight;
        _rightTitleLabel.font = APPFONT(14);
        _rightTitleLabel.text = @"全部订单";
        [self.titleBgView addSubview:_rightTitleLabel];
        [_rightTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.rightArrowImageView.mas_left).with.offset(- 5);
            make.centerY.equalTo(self.rightArrowImageView);
        }];
    }
    return _rightTitleLabel;
}
- (UIImageView *)rightArrowImageView {
    if (!_rightArrowImageView) {
        _rightArrowImageView = [UIImageView new];
        UIImage *image = IMAGE_NAME(@"user_home_sectionRightArrow");
        [_rightArrowImageView setImage:image];
        [self.titleBgView addSubview:_rightArrowImageView];
        [_rightArrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.titleBgView).with.offset(- 15);
            make.centerY.equalTo(self.titleLabel);
            make.width.mas_equalTo(image.size.width);
            make.height.mas_equalTo(image.size.height);
        }];
    }
    return _rightArrowImageView;
}
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 0.5;
        flowLayout.minimumInteritemSpacing = 0.5;
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.scrollEnabled = NO;
        
        [_collectionView registerClass:[UserCenterHomeTVCCollectionViewCell class] forCellWithReuseIdentifier:[UserCenterHomeTVCCollectionViewCell cellClass]];
        
        [self.contentView addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleBgView.mas_bottom);
            make.left.and.bottom.and.right.equalTo(self.contentView);
        }];
    }
    return _collectionView;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
